var loadIndex = null;
var layer = null;
var pageIsClick = "";
layui.use('layer', function() {
    layer = layui.layer;
});
$("#exit-login").on("click", function() {
    ///login/logout
    var index = layer.confirm("<span style='color:indianred'>确认退出登录？</span>", { icon: 0 }, function() {
        $.ajax({
            url: "/login/logout",
            type: "post",
            dataType: "json",
            beforeSend: function() {
                layer.load(0);
            },
            success: function(d) {
                if (d.result == "1") {
                    if (window.location.protocol == "http:") {
                        window.location.href = "http://" + window.location.host + "/login/guard";
                    } else if (window.location.protocol == "https:") {
                        window.location.href = "https://" + window.location.host + "/login/guard";
                    }
                } else {

                    layer.close(loadIndex);
                    layer.close(index);
                    layer.msg("操作失败！", { icon: 2, time: 2000 });
                }
            },
            error: function() {
                layer.close(loadIndex);
                layer.close(index);
                layer.msg("网络异常！", { icon: 2, time: 2000 });
            }
        });
    });
});
var nav_value = [
    // {
    //     'title':'主页',
    //     'src':'/index/homepage'
    // },
    {
        'title': '主页',
        // 'child': [{
        //     'title': '首页统计',
        //     'src': '/index/hometotal'
        // }, {
        //     'title': '首页管理',
        //     'src': '/index/homepage'
        // }]
        "src": "/index/hometotal"
    },
    {
        'title': 'APP管理',
        'child': [{
            'title': '添加APP',
            'src': '/index/add_app'
        }, {
            'title': 'app列表',
            'src': '/index/app_list'
        }, {
            'title': '马甲包列表',
            'src': '/index/majia_list'
        }]
    },
    {
        'title': '渠道商管理',
        'child': [{
                'title': '渠道商管理',
                'src': '/index/channel_manage'
            },
            {
                'title': '添加渠道商',
                'src': '/index/channel_add'
            }
        ]
    },
    {
        'title': '用户管理',
        'child': [{
            'title': '用户列表',
            'src': '/index/user_list'
        }]
    },
    {
        'title': '推送管理',
        'child': [{
            'title': '推送消息',
            'src': '/index/push_msg'
        }, {
            'title': '推送列表',
            'src': '/index/push_list'
        }]
    },
    {
        'title': '统计管理',
        'child': [{
                'title': '渠道统计',
                'src': '/index/channel_total'
            }, {
                'title': '产品统计',
                'src': '/index/product_total'
            },
            {
                'title': '流量统计',
                'src': '/index/traffic_total'
            }, {
                'title': '操作统计',
                'src': '/index/opraty_total'
            }
        ]
    }, {
        'title': '管理员设置',
        'child': [{
            'title': '管理员列表',
            'src': '/index/admin_list'
        }, {
            'title': '添加管理员',
            'src': '/index/admin_add'
        }]
    },
    {
        'title': '站点设置',
        'child': [{
                'title': '意见反馈',
                'src': '/index/user_feedback'
            }, {
                'title': '修改密码',
                'src': '/index/change_password'
            },
            {
                'title': '模块域名绑定',
                'src': '/index/block_url_bind'
            }, {
                'title': '手机幻灯片管理',
                'src': '/index/phone_slide_manage'
            },
            {
                'title': '清空缓存',
                'src': '/index/clear_cache'
            }
        ]
    },
];
//默认请求第一个页面
$.ajax({
    type: 'get',
    url: nav_value[0].src || nav_value[0].child[0].src,
    data: {},
    dataType: 'html',
    async: true,
    beforeSend: function() {
        loadIndex = layer.load(0, { shade: 0.8 });
    },
    success: function(d) {
        pageIsClick = nav_value[0].src || nav_value[0].child[0].src;
        layer.close(loadIndex);
        page_index.max = $('.admin-nav-ele').eq(0).text();
        page_index.min = nav_value[0].src == undefined ? $(".admin-nav-floatWin").eq(0).find("a").text() : "";
        $("#page-root").text(page_index.max + page_index.min);
        $('#page-container').html("").html(d);
    },
    error: function(d) {
        $('#page-container').html(d);
        pageIsClick = "";
        page_index.max = "";
        page_index.min = "";
        $("#page-root").text("");
    }
});

for (var i = 0; i < nav_value.length; i++) {
    var div = document.createElement('div');
    div.className = "admin-nav-ele";
    div.innerText = nav_value[i].title;
    div.addEventListener('mouseover', windowFloatShow);
    div.addEventListener('mouseout', windowFloatHide);
    div.setAttribute('_src', nav_value[i].src);
    if (nav_value[i].child != undefined) {

        var div2 = document.createElement('div');
        div2.className = 'admin-nav-floatWin';
        div.index = i;
        div.setAttribute('isList', 'true');
        div2.setAttribute('index', i);


        for (var j = 0; j < nav_value[i].child.length; j++) {
            div2.innerHTML += '<a _src="' + nav_value[i].child[j].src + '" index="' + i + '">' + nav_value[i].child[j].title + '</a>';
        }
        $('body').eq(0).append(div2);
    } else {

        div.setAttribute('isList', 'false');
        div.addEventListener('click', function(e) {
            e.stopPropagation();
            if ($(this).attr("_src") != pageIsClick) {
                var _src = $(this).attr("_src");
                page_index.max = $(this).text();
                $("#page-root").text(page_index.max + page_index.min);
                $.ajax({
                    type: 'get',
                    url: $(this).attr("_src"),
                    data: {},
                    dataType: 'html',
                    beforeSend: function() {
                        loadIndex = layer.load(0, { shade: 0.8 });
                    },
                    success: function(d) {
                        layer.close(loadIndex);
                        $('#page-container').html("").html(d);
                        pageIsClick = _src;
                    },
                    error: function(d) {
                        $('#page-container').html(d);
                        pageIsClick = "";
                        page_index.max = "";
                        page_index.min = "";
                        $("#page-root").text("");
                    }
                });
            }
        });
    }
    $('#center-nav').append(div);
}

//当前页面
var page_index = {
    max: '',
    min: ''
}

function windowFloatShow(e) {

    var that = this;
    $('.admin-nav-floatWin').each(function(e, i) {
        if (that.getAttribute('isList') == "false") {
            $('.admin-nav-floatWin').hide();
        }
        if (that.index == Number(i.getAttribute('index'))) {
            $('.admin-nav-floatWin').eq(e).css('left', that.getBoundingClientRect().left);
            $('.admin-nav-floatWin').eq(e).show();
            page_index.max = $(that).text();
        } else {
            $('.admin-nav-floatWin').eq(e).hide();
            page_index.max = "";
            page_index.min = "";
        }
    });
}

function windowFloatHide(e) {
    $('.admin-nav-floatWin').hide();
}
$('.admin-nav-floatWin').on('mouseover', function(e) {
    e.stopPropagation();
    $(this).show();
});
$('.admin-nav-floatWin').on('mouseout', function(e) {
    e.stopPropagation();
    $(this).hide();
});
$('.admin-nav-floatWin a').on('click', function(e) {
    e.stopPropagation();
    if ($(this).attr("_src") != pageIsClick) {
        var index = null;
        var _src = $(this).attr("_src");
        page_index.max = $('.admin-nav-ele').eq(Number($(this).attr("index"))).text();
        page_index.min = " / " + $(this).text();
        $("#page-root").text(page_index.max + page_index.min);
        $.ajax({
            type: 'get',
            url: $(this).attr("_src"),
            data: {},
            dataType: 'html',
            beforeSend: function() {
                loadIndex = layer.load(0, { shade: 0.8 });
            },
            success: function(d) {
                layer.close(loadIndex);
                $('#page-container').html("").html(d);
                pageIsClick = _src;
            },
            error: function(d) {
                $('#page-container').html(d);
                pageIsClick = "";
                page_index.max = "";
                page_index.min = "";
                $("#page-root").text("");
            }
        });
    }

});
//全屏按钮
$('#full_screen_btn').on('click', function() {
    var requestMethod = $('#full_screen')[0].requestFullScreen || //W3C
        $('#full_screen')[0].webkitRequestFullScreen || //FireFox
        $('#full_screen')[0].mozRequestFullScreen || //Chrome等
        $('#full_screen')[0].msRequestFullScreen; //IE11
    if (requestMethod) {
        requestMethod.call($('#full_screen')[0]);
    } else if (typeof window.ActiveXObject !== "undefined") { //for Internet Explorer
        var wscript = new ActiveXObject("WScript.Shell");
        if (wscript !== null) {
            wscript.SendKeys("{F11}");
        }
    }
});